package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.UserMessageDO;
import com.yuantu.judiciary.model.enums.MessageType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;

/**
 * 用户消息
 *
 * @author syw
 * @since 2023-08-23
 **/
public interface IUserMessageDao extends JpaRepository<UserMessageDO, Long> {

    Integer countDistinctByCreatorIdAndReadedFalseAndDeletedFalse(Long creatorId);

    List<UserMessageDO> getUserMessageByCreatorIdAndDeletedFalse(Long creatorId);

    Page<UserMessageDO> findUserMessageByCreatorIdAndTypeAndDeletedFalse(Long creatorId, MessageType type, Pageable pageable);

    @Modifying
    @Query("update UserMessageDO set readed = true, modifierId = :accountId, modifyTime = :currentTime " +
            "where id = :id and deleted = false ")
    void readMessageById(Long id, Long accountId, Timestamp currentTime);

    @Modifying
    @Query("update UserMessageDO set readed = true, modifierId = :accountId, modifyTime = :currentTime " +
            "where creatorId = :accountId and type = :type and deleted = false ")
    void readAllMessageByType(Long accountId, MessageType type, Timestamp currentTime);

}
